Imports System.Data

Partial Class WebForms_Beneficiary
    Inherits WebFormCommon

    Dim strPath As String

    Private Sub SetPageSecurity()
        Dim sec As New Secure
        Dim cfunc As New CIMSfunc

        sec.ParentPage = ParentPage
        sec.SetPageAccess(Session("UserName"))

        If ViewState("mode") = "view" Then
            cfunc.FormDisable(divMain)
        End If

        cfunc = Nothing
        sec = Nothing

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Share_txt.Attributes.Add("onkeypress", "return checkDecimal(this);")

        Select Case Me.PageControlMode
            Case ControlMode.NewBusiness
                strPath = "New_Business"
            Case ControlMode.ClientServicing
                strPath = "Client_Servicing"
        End Select

        If Not Page.IsPostBack Then
            Dim lookup As New LookUp.FillLookUp

            With lookup
                .FillDropDown(Country_cmb, .eLookUpType.country)
                .FillDropDown(City_cmb, .eLookUpType.country_city, , Country_cmb.SelectedValue.ToString)
                .FillDropDown(Gender_cmb, .eLookUpType.other, "GENDER")
                .FillDropDown(Rel_cmb, .eLookUpType.other, "RELEATIONSHIP")
                .FillDropDown(Ranking_cmb, .eLookUpType.other, "BENEFICIARY_RANK")
                .FillDropDown(IDType_cmb, .eLookUpType.other, "ID_TYPE")
            End With

            Call InitVars()
            Policy_td.InnerText = ViewState("PolicyNo")
            Provider_td.InnerText = ViewState("Provider")
            Name_td.InnerText = ViewState("CName")
            Call SetPageSecurity()
            Call LoadBene()

            Dim sec As New Secure

            sec.ParentPage = ParentPage

            If IsNothing(ViewState("CID")) Then
                sec.AddControl(Save_btn, Secure.ControlTypes.AddType)
            Else
                sec.AddControl(Save_btn, Secure.ControlTypes.EditType)
            End If

            sec.SetPageAccess(Session("UserName"))
            sec = Nothing
        End If
    End Sub

    Private Sub LoadBene()
        If IsNothing(ViewState("CID")) Then Exit Sub

        Dim dao As New DAOMain.POLICY_BENE_DTL_DAO
        Dim vo As New DAOMain.POLICY_BENE_DTL_VO
        Dim cFunc As New CIMSfunc
        Dim LookUp As New LookUp.FillLookUp

        vo.POLICY_BENE_CD = ViewState("CID")
        vo.POLICY_SEQ_CD = ViewState("Seq_CD")

        Dim dt As DataTable = dao.Search(vo)

        If dt.Rows.Count > 0 Then
            With dt.Rows(0)

                Birthday_txt.Text = cFunc.ParseGetDate(.Item("BIRTHDAY"))
                Country_cmb.SelectedValue = .Item("COUNTRY_CD")
                LookUp.FillDropDown(City_cmb, LookUp.eLookUpType.country_city, , Country_cmb.SelectedValue.ToString)

                City_cmb.SelectedValue = .Item("CITY_CD")
                Gender_cmb.SelectedValue = .Item("GENDER")
                ID_Num.Text = .Item("ID_NO")
                IDType_cmb.SelectedValue = .Item("ID_TYPE")
                Rel_cmb.SelectedValue = .Item("RELATIONSHIP")
                Occupation_txt.Text = .Item("OCCUPATION")
                Add1_txt.Text = .Item("POLICY_BENE_ADDRESS")
                Add2_txt.Text = .Item("POLICY_BENE_ADDRESS2")
                FirstName_txt.Text = .Item("POLICY_BENE_FIRST_NAME")
                LastName_txt.Text = .Item("POLICY_BENE_LAST_NAME")
                MiddleName_txt.Text = .Item("POLICY_BENE_MIDDLE_NAME")
                Ranking_cmb.SelectedValue = .Item("RANKING_CD")
                Share_txt.Text = .Item("SHARE")
                Zip_txt.Text = .Item("ZIP_CD")

                T1_txt.Text = cFunc.ParsePhoneNo(.Item("TEL_NO"), 1)
                T2_txt.Text = cFunc.ParsePhoneNo(.Item("TEL_NO"), 2)
                T3_txt.Text = cFunc.ParsePhoneNo(.Item("TEL_NO"), 3)
                T4_txt.Text = cFunc.ParsePhoneNo(.Item("TEL_NO"), 4)

                M1_txt.Text = cFunc.ParsePhoneNo(.Item("MOBILE_NO"), 1)
                M2_txt.Text = cFunc.ParsePhoneNo(.Item("MOBILE_NO"), 2)
                M3_txt.Text = cFunc.ParsePhoneNo(.Item("MOBILE_NO"), 3)

            End With
        End If

        LookUp = Nothing
        cFunc = Nothing
        dao = Nothing
        vo = Nothing
    End Sub

    Private Function GetBeneficiaryTotals() As Double
        Dim dm As New DAOMain.CommonDAO
        Dim iReturn As Double

        dm.AddParameter("Beneficiary")
        dm.AddParameter(ViewState("Seq_CD"))
        dm.AddParameter(Val(Share_txt.Text))
        If Not IsNothing(ViewState("CID")) Then
            dm.AddParameter(ViewState("CID"))
        Else
            dm.AddParameter("")
        End If
        dm.AddParameter(Ranking_cmb.SelectedValue)

        Dim dt As DataTable = dm.ExecQryProc("sp_get_total_percent")

        If dt.Rows.Count > 0 Then
            iReturn = dt.Rows(0).Item("Percentage")
        End If

        Return iReturn
        dm = Nothing
    End Function

    Protected Sub Save_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Save_btn.Click
        Dim dao As New DAOMain.POLICY_BENE_DTL_DAO
        Dim vo As New DAOMain.POLICY_BENE_DTL_VO
        Dim cFunc As New CIMSfunc
        Dim polyFunc As New PolyFunc.ASPFunc

        If GetBeneficiaryTotals() > 100 Then
            polyFunc.MessageAlert(ParentPage, "Funds total percentage exceeds 100%")
            Exit Sub
        End If

        With vo
            .POLICY_SEQ_CD = ViewState("Seq_CD")

            .BIRTHDAY = cFunc.ParseSetDate(Birthday_txt.Text)
            .CITY_CD = City_cmb.SelectedValue.ToString
            .COUNTRY_CD = Country_cmb.SelectedValue.ToString
            .GENDER = Gender_cmb.SelectedValue.ToString
            .ID_NO = ID_Num.Text
            .ID_TYPE = IDType_cmb.SelectedValue.ToString
            .OCCUPATION = Occupation_txt.Text
            .POLICY_BENE_ADDRESS = Add1_txt.Text
            .POLICY_BENE_ADDRESS2 = Add2_txt.Text
            .POLICY_BENE_FIRST_NAME = FirstName_txt.Text
            .POLICY_BENE_LAST_NAME = LastName_txt.Text
            .POLICY_BENE_MIDDLE_NAME = MiddleName_txt.Text
            .RANKING_CD = Ranking_cmb.SelectedValue.ToString
            .RELATIONSHIP = Rel_cmb.SelectedValue.ToString
            .SHARE = Val(Share_txt.Text)
            .ZIP_CD = Zip_txt.Text
            .MOBILE_NO = M1_txt.Text & "-" & M2_txt.Text & "-" & M3_txt.Text
            .TEL_NO = T1_txt.Text & "-" & T2_txt.Text & "-" & T3_txt.Text & "-" & T4_txt.Text

            Select Case IsNothing(ViewState("CID"))
                Case True
                    .POLICY_BENE_CD = cFunc.GetLastID("POLICY_BENE_CD", "POLICY_BENE_DTL")
                    .POLICY_BENE_CREA_BY = Session("UserName")
                    .POLICY_BENE_CREA_DT = Now

                    If dao.Create_Record(vo) Then
                        InitSessions()
                        Response.Redirect("~/" & strPath & "/ClientInfo.aspx")
                    Else
                        polyFunc.MessageAlert(ParentPage, "Error Saving Record!")
                    End If
                Case False
                    .POLICY_BENE_CD = ViewState("CID")
                    .POLICY_BENE_LAST_UPD_BY = Session("UserName")
                    .POLICY_BENE_LAST_UPD_DT = Now

                    If dao.Update(vo) Then
                        InitSessions()
                        Response.Redirect("~/" & strPath & "/ClientInfo.aspx")
                    Else
                        polyFunc.MessageAlert(ParentPage, "Error Saving Record!")
                    End If
            End Select
        End With

        polyFunc = Nothing
        cFunc = Nothing
        dao = Nothing
        vo = Nothing
    End Sub

    Protected Sub Cancel_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Cancel_btn.Click
        InitSessions()
        Response.Redirect("~/" & strPath & "/ClientInfo.aspx")
    End Sub

    Protected Sub Country_cmb_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Country_cmb.SelectedIndexChanged
        Dim lookup As New LookUp.FillLookUp

        lookup.FillDropDown(City_cmb, lookup.eLookUpType.country_city, , Country_cmb.SelectedValue.ToString)
        lookup = Nothing
    End Sub

    Protected Sub lbtnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtnSearch.Click
        ClientSearch1.Visible = True
    End Sub

    Protected Sub ClientSearch_Policy_Selected() Handles ClientSearch1.Policy_Selected
        Dim cFunc As New CIMSfunc
        Dim polyFunc As New PolyFunc.ASPFunc
        Dim LookUp As New LookUp.FillLookUp

        With ClientSearch1
            Birthday_txt.Text = cFunc.ParseGetDate(.dtBday)
            Country_cmb.SelectedValue = .strCountry
            LookUp.FillDropDown(City_cmb, LookUp.eLookUpType.country_city, , Country_cmb.SelectedValue.ToString)

            City_cmb.SelectedValue = .strCity
            Add1_txt.Text = .strAdd1
            Add2_txt.Text = .strAdd2
            FirstName_txt.Text = .strFirstName
            LastName_txt.Text = .strLastName
            MiddleName_txt.Text = .strMiddleName
            Gender_cmb.SelectedValue = .strGender

            .Visible = False
        End With

        LookUp = Nothing
        cFunc = Nothing
        polyFunc = Nothing
    End Sub

End Class
